package com.chen.lifepic.utils;

import java.io.File;
import java.io.IOException;
import java.security.MessageDigest;
import java.util.Date;

import android.content.ComponentName;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.pm.PackageInfo;
import android.content.res.Configuration;
import android.graphics.Bitmap;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.util.Log;

public class Utils {
	public static final boolean ENABLE_LOG = false;

	public static final boolean ENABLE_ADS = false;

	public static String formatTime(long time) {
		int hour = (int) (time / 3600000);
		int min = (int) (time / 60000 - hour * 60);
		int sec = (int) (time / 1000 - hour * 3600 - min * 60);
		int milli = (int) (time - hour * 3600000 - min * 60000 - sec * 1000);

		return formatZero(2, hour) + ":" + formatZero(2, min) + ":"
				+ formatZero(2, sec) + ":" + formatZero(3, milli);
	}

	public static String formatZero(int median, int time) {
		if ((median == 2 && time < 10)
				|| (median == 3 && time < 100 && time >= 10)) {
			return "0" + time;
		} else if (median == 3 && time < 10) {
			return "00" + time;
		}
		return "" + time;
	}

	public static String formatStandardTime(long time) {
		try {
			Date date = new Date(time);
			java.text.DateFormat format = new java.text.SimpleDateFormat(
				"yyyy-MM-dd kk:mm:ss");
			return format.format(date);

		} catch (Exception e) {
			e.printStackTrace();
		}

		return "";
	}

	public static String formatStandardDateDay(long time) {
		try {
			Date date = new Date(time);
			java.text.DateFormat format = new java.text.SimpleDateFormat(
				"yyyy-MM-dd");
			return format.format(date);

		} catch (Exception e) {
			e.printStackTrace();
		}

		return "";
	}

	public static String formatStandardDateClock(long time) {
		try {
			Date date = new Date(time);
			java.text.DateFormat format = new java.text.SimpleDateFormat(
				"kk:mm:ss");
			return format.format(date);

		} catch (Exception e) {
			e.printStackTrace();
		}

		return "";
	}

	public static void logDebug(String tag, String msg) {
		if (ENABLE_LOG) {
			Log.d(tag, msg);
		}
	}

	// public static void share(Context context, String time, String title) {
	// if (null == context || null == title) {
	// return;
	// }
	// try {
	// Intent intent = new Intent(Intent.ACTION_SEND);
	// intent.setType("text/plain");
	// intent.putExtra(Intent.EXTRA_SUBJECT, context.getResources()
	// .getString(R.string.share_title, time));
	// intent.putExtra(Intent.EXTRA_TEXT, title);
	// context.startActivity(intent);
	//
	// } catch (Exception e) {
	// e.printStackTrace();
	// }
	// }

	public static String getPkgVersionName(Context context, Class cls) {
		try {
			ComponentName comp = new ComponentName(context, cls);
			PackageInfo pinfo = context.getPackageManager().getPackageInfo(
				comp.getPackageName(), 0);

			return null == pinfo ? null : pinfo.versionName;
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}

	public static long getDateDay(long time) {
		long day = time - time % 86400000;
		return day;
	}

	public static boolean isLandScape(Context context) {
		if (null == context) {
			return false;
		}
		if (context.getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE) {
			return true;
		} else {
			return false;
		}
	}

	public static Bitmap resizeImage(Bitmap bitmap, int w, int h) {
		// load the origial Bitmap
		Bitmap BitmapOrg = bitmap;
		int width = BitmapOrg.getWidth();
		int height = BitmapOrg.getHeight();
		int newWidth = w;
		int newHeight = h;
		// calculate the scale

		float scaleWidth = ((float) newWidth) / width;
		float scaleHeight = ((float) newHeight) / height;
		// create a matrix for the manipulation
		Matrix matrix = new Matrix();
		// resize the Bitmap

		matrix.postScale(scaleWidth, scaleHeight);
		// if you want to rotate the Bitmap
		// matrix.postRotate(45);
		// recreate the new Bitmap

		Bitmap resizedBitmap = Bitmap.createBitmap(
			BitmapOrg, 0, 0, width, height, matrix, true);
		// make a Drawable from Bitmap to allow to set the Bitmap
		// to the ImageView, ImageButton or what ever
		return resizedBitmap;
	}

	public static int getFontHeight(Paint paint) {
		if (null == paint) {
			return 0;
		}
		return (int) Math.ceil(paint.descent() - paint.ascent());
	}

	public static void sharedPreferences(Context context, String key,
			boolean value) {
		if (null == context) {
			return;
		}
		SharedPreferences preferences = context.getSharedPreferences(
			context.getPackageName(), Context.MODE_PRIVATE);
		if (null == preferences) {
			return;
		}

		preferences.edit().putBoolean(key, value).commit();
	}

	public static boolean getBooleanValueFromSharedPreferences(Context context,
			String key, boolean defaultValue) {
		if (null == context) {
			return defaultValue;
		}
		SharedPreferences preferences = context.getSharedPreferences(
			context.getPackageName(), Context.MODE_PRIVATE);
		if (null == preferences) {
			return defaultValue;
		}

		return preferences.getBoolean(key, defaultValue);
	}

	public static boolean isFileExist(String path) {
		File file = new File(path);
		return file.exists();
	}

	public static File createDirIfNotExist(String path) {
		File dir = new File(path);
		dir.mkdir();
		return dir;
	}

	public static File createFileIfNotExist(String path) throws IOException {
		File file = new File(path);
		file.createNewFile();
		return file;
	}

	public String downloadName(String url) {
		return MD5(url);
	}

	public final static String MD5(String s) {
		char hexDigits[] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
				'A', 'B', 'C', 'D', 'E', 'F' };
		try {
			byte[] btInput = s.getBytes();
			MessageDigest mdInst = MessageDigest.getInstance("MD5");
			mdInst.update(btInput);
			byte[] md = mdInst.digest();
			int j = md.length;
			char str[] = new char[j * 2];
			int k = 0;
			for (int i = 0; i < j; i++) {
				byte byte0 = md[i];
				str[k++] = hexDigits[byte0 >>> 4 & 0xf];
				str[k++] = hexDigits[byte0 & 0xf];
			}
			return new String(str);
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}

	// public static void keepScreenOnOrOff(Activity activity, boolean on) {
	// if (null == activity)
	// return;
	// android.os.PowerManager manager = (PowerManager)
	// activity.getSystemService(Context.POWER_SERVICE);
	//
	// if (null == manager)
	// return;
	//
	// WakeLock lock = manager.newWakeLock(
	// PowerManager.FLAG_KEEP_SCREEN_ON , "My Lock");
	//
	// if (null == lock)
	// return;
	//
	// if (on) {
	// lock.acquire();
	// } else {
	// lock.setReferenceCounted(false);
	// lock.release();
	// }
	// }
}
